package com.example.administrator.testxingpiao.ui.detail;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.testxingpiao.R;
import com.example.administrator.testxingpiao.base.BaseActivity;
import com.example.administrator.testxingpiao.bean.CartBean;
import com.example.administrator.testxingpiao.bean.CollectorBean;
import com.example.administrator.testxingpiao.bean.GoodsDetailBean;
import com.example.administrator.testxingpiao.config.ConfigUrl;
import com.example.administrator.testxingpiao.config.Constant;
import com.example.administrator.testxingpiao.dao.CartDao;
import com.example.administrator.testxingpiao.dao.CollectDao;
import com.example.administrator.testxingpiao.ui.cart.CartActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/8/8.
 */
public class PerformDetailActivity extends BaseActivity implements DetailContract.View {


    @BindView(R.id.detail_backup_perfromdetail)
    ImageView detailBackupPerfromdetail;
    @BindView(R.id.iv_show_detail)
    ImageView ivShowDetail;
    @BindView(R.id.tv_title_show_detail)
    TextView tvTitleShowDetail;
    @BindView(R.id.tv_price_show_detail)
    TextView tvPriceShowDetail;
    @BindView(R.id.tv_price_value_detail)
    TextView tvPriceValueDetail;
    @BindView(R.id.tv_price_market_show_detail)
    TextView tvPriceMarketShowDetail;
    @BindView(R.id.tv_price_market_value_detail)
    TextView tvPriceMarketValueDetail;
    @BindView(R.id.tv_sales_show_detail)
    TextView tvSalesShowDetail;
    @BindView(R.id.tv_sales_value_detail)
    TextView tvSalesValueDetail;
    @BindView(R.id.tv_counts_show_detail)
    TextView tvCountsShowDetail;
    @BindView(R.id.tv_counts_value_detail)
    TextView tvCountsValue;
    @BindView(R.id.tv_season_detail)
    TextView tvSeason;
    @BindView(R.id.tv_price_detail)
    TextView tvPrice;
    @BindView(R.id.iv_count_left)
    ImageView ivCountLeft;
    @BindView(R.id.tv_count_middle)
    TextView tvSelectCount;
    @BindView(R.id.iv_count_right)
    ImageView ivCountRight;
    @BindView(R.id.bt_buy_performdetail)
    Button btBuyPerform;
    @BindView(R.id.btn_collect)
    Button btnCollect;
    @BindView(R.id.cart_count_detial)
    TextView tvCartCount;
    @BindView(R.id.btn_add_cart)
    Button btnAddCart;
    @BindView(R.id.bt_collect_show_detail)
    Button btCollectShow;
    @BindView(R.id.iv_cart)
    ImageView ivCart;
    @BindView(R.id.rl_cart)
    RelativeLayout rlCart;

    private DetailModel model;
    private DetailPresenter presenter;
    private int countSelect = 0;
    private int countCart = 0;
    private String name = "";
    private String price = "";
    private String url = "";
    private String image = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_perform_detail);
        ButterKnife.bind(this);
        initData();
        initBoxSelected();
        initCountSelect();
        initCartCount();
    }

    public void initData() {

        model = new DetailModel();
        presenter = new DetailPresenter(this, model);
        presenter.getData(initPath(), Constant.TYPE_PERFORM);
    }

    private String initPath() {
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String path = ConfigUrl.Path.BASE_URL + ConfigUrl.Path.Detail_URL +
                "?act=goods&op=goods_detail&goods_id=" + id +
                "&key=0d7d06f87a82391622f02d10002750ec";
        return path;
    }

    @Override
    public void onGetGoodsDetail(final GoodsDetailBean goodsDetailBean) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                image = goodsDetailBean.getImage();
                price = goodsDetailBean.getCostPrice();
                name = goodsDetailBean.getGoods_name();
                url = goodsDetailBean.getUrl();
                String goodsStorage = goodsDetailBean.getGoodsStorage();
                String marketPrice = goodsDetailBean.getMarketPrice();
                String saleNum = goodsDetailBean.getSaleNum();
                String season = goodsDetailBean.getSeason();
                Glide.with(PerformDetailActivity.this).load(image).into(ivShowDetail);

                tvTitleShowDetail.setText(name);
                tvPriceValueDetail.setText(price);
                tvPrice.setText(price);
                tvPriceMarketValueDetail.setText(marketPrice);
                tvSalesValueDetail.setText(saleNum);
                tvCountsValue.setText(goodsStorage);
                tvSeason.setText(season);
            }
        });
    }
    @OnClick({R.id.detail_backup_perfromdetail,R.id.rl_cart, R.id.bt_collect_show_detail,R.id.tv_season_detail, R.id.tv_price_detail, R.id.iv_count_left, R.id.iv_count_right, R.id.bt_buy_performdetail, R.id.btn_collect, R.id.btn_add_cart})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.detail_backup_perfromdetail:
                finish();
                break;
            case R.id.bt_collect_show_detail:
                collect();
                break;
            case R.id.tv_season_detail:
                tvSeason.setSelected(!tvSeason.isSelected());
                break;
            case R.id.tv_price_detail:
                tvPrice.setSelected(!tvPrice.isSelected());
                break;
            case R.id.iv_count_left:
                if (countSelect == 0) {
                    Toast.makeText(PerformDetailActivity.this, "数量为0了，你还点！", Toast.LENGTH_SHORT).show();
                } else {
                    countSelect--;
                    showSelectCount();
                }
                break;
            case R.id.iv_count_right:
                countSelect++;
                showSelectCount();
                break;
            case R.id.bt_buy_performdetail:
                break;
            case R.id.btn_collect:
                collect();
                break;
            case R.id.btn_add_cart:
                countCart += countSelect;
                CartBean cart = new CartBean(name, price, String.valueOf(countCart), image);
                CartBean cartBean = CartDao.queryOne(PerformDetailActivity.this, cart);
                if(cartBean != null){
                    updataCount(cart);
                    return;
                }
                int addCart = CartDao.add(cart, this);
                if (addCart != -1) {
                    showCartCount();
                    Toast.makeText(PerformDetailActivity.this, "已加入购物车", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(PerformDetailActivity.this, "失败了", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rl_cart:
                Intent intent = new Intent(this, CartActivity.class);
                startActivity(intent);
                break;
        }
    }
    private void updataCount(CartBean cart) {
        int update = CartDao.update(PerformDetailActivity.this, cart);
        if(update != -1){
            showCartCount();
            Toast.makeText(PerformDetailActivity.this, "已加入过购物车，更改数量成功！", Toast.LENGTH_SHORT).show();
        }
    }

    private void initBoxSelected() {
        tvSeason.setSelected(true);
        tvPrice.setSelected(true);
    }

    private void initCountSelect() {
        countSelect = Integer.parseInt(tvSelectCount.getText().toString());
    }

    private void initCartCount() {
        List<CartBean> carts = CartDao.queryAll(this);
        countCart = carts.size();
        showCartCount();
    }

    private void showSelectCount() {
        tvSelectCount.setText(String.valueOf(countSelect));
    }

    private void showCartCount() {
        tvCartCount.setText(String.valueOf(countCart));
    }
    private void collect(){
        CollectorBean cb = new CollectorBean(name, price, url);
        CollectorBean one = CollectDao.queryOne(PerformDetailActivity.this, cb);
        if(one != null){
            Toast.makeText(PerformDetailActivity.this, "已经收藏过了！", Toast.LENGTH_SHORT).show();
            return;
        }
        int add = CollectDao.add(cb, this);
        if (add != -1) {
            Toast.makeText(PerformDetailActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(PerformDetailActivity.this, "失败了", Toast.LENGTH_SHORT).show();
        }
    }
}
